package xnj.lazydog.btcontroller.ControlViews;

import java.io.Serializable;

public class Value implements Serializable
{
    private boolean bl;
    private byte b;
    private short s;
    private int i;
    private float f;
    public int type=0;
    public Value(int type)
    {
        this.type = type;
    }
    public void set(boolean b) {

        switch (type) {
            case 0:
                bl = b;
                break;
            case 1:
                this.b = (byte) (b ? 1 : 0);
                break;
            case 2:
                this.s = (short) (b ? 1 : 0);
                break;
            case 3:
                this.i = (b ? 1 : 0);
                break;
            case 4:
                this.f = (float) (b ? 1 : 0);
                break;
        }
    }
    public void set(byte b)
    {
        switch (type)
        {
            case 0:
                bl = b>0.5f;
                break;
            case 1:
                this.b = b;
                break;
            case 2:
                this.s = b;
                break;
            case 3:
                this.i = b;
                break;
            case 4:
                this.f = b;
                break;
        }
    }

    private int tmp;
    public void set(short b)
    {

        switch (type)
        {
            case 0:
                bl = b>0.5f;
                break;
            case 1:
                tmp = b;
                if(tmp>127)
                    tmp = 127;
                if(tmp<-128)
                    tmp = -128;

                this.b = (byte) tmp;
                break;
            case 2:
                this.s = b;
                break;
            case 3:
                this.i = b;
                break;
            case 4:
                this.f = (float)(b);
                break;
        }
    }

    public void set(int b)
    {

        switch (type) {
            case 0:
                bl = b > 0.5f;
                break;
            case 1:
                tmp = b;
                if (tmp > 127)
                    tmp = 127;
                if (tmp < -128)
                    tmp = -128;
                this.b = (byte) (tmp);
                break;
            case 2:
                tmp = b;
                if (tmp > 32767)
                    tmp = 32767;
                if (tmp < -32768)
                    tmp = -32768;

                this.s = (short) (tmp);
                break;
            case 3:
                this.i = b;
                break;
            case 4:
                this.f = (float) (b);
                break;
        }
    }


    private float tmp_f;
    public void set(float b)
    {

        switch (type)
        {
            case 0:
                bl = b>0.5f;
                break;
            case 1:
                tmp_f = b;
                if(tmp_f>127)
                    tmp_f = 127;
                if(tmp_f<-128)
                    tmp_f = -128;
                this.b = (byte) (tmp_f);
                break;
            case 2:
                tmp_f = b;
                if(tmp_f>32767)
                    tmp_f = 32767;
                if(tmp_f<-32768)
                    tmp_f = -32768;
                this.s = (short) (tmp_f);
                break;
            case 3:
                this.i = (int)(b);
                break;
            case 4:
                this.f = (b);
                break;
        }
    }

    public boolean getBoolean()
    {
        switch (type)
        {
            case 0:
                return bl;
            case 1:
                return b>0.5f;
            case 2:
                return s>0.5f;
            case 3:
                return i>0.5f;
            case 4:
                return f>0.5f;
        }
        return false;
    }

    public boolean getBoolean(float threshold)
    {
        switch (type)
        {
            case 0:
                return bl;
            case 1:
                return b>threshold;
            case 2:
                return s>threshold;
            case 3:
                return i>threshold;
            case 4:
                return f>threshold;
        }
        return false;
    }


    public byte getByte()
    {

        switch (type)
        {
            case 0:
                return (byte)(bl?1:0);
            case 1:

                return b;
            case 2:
                tmp = s;
                if (tmp > 127)
                    tmp = 127;
                if (tmp < -128)
                    tmp = -128;
                return (byte)tmp;
            case 3:

                tmp = i;
                if (tmp > 127)
                    tmp = 127;
                if (tmp < -128)
                    tmp = -128;
                return (byte)tmp;

            case 4:
                tmp = (int)f;
                if (tmp > 127)
                    tmp = 127;
                if (tmp < -128)
                    tmp = -128;
                return (byte)tmp;
        }
        return 0;
    }

    public short getShort()
    {

        switch (type)
        {
            case 0:
                return (short) (bl?1:0);
            case 1:

                return b;
            case 2:
                return s;
            case 3:

                tmp = i;
                if (tmp > 32767)
                    tmp = 32767;
                if (tmp < -32768)
                    tmp = -32768;
                return (short)tmp;

            case 4:
                tmp = (int)f;
                if (tmp > 32767)
                    tmp = 32767;
                if (tmp < -32768)
                    tmp = -32768;
                return (short) tmp;
        }
        return 0;
    }
    public int getInt()
    {

        switch (type)
        {
            case 0:
                return (bl?1:0);
            case 1:
                return b;
            case 2:
                return s;
            case 3:
                return i;
            case 4:
                return (int)f;
        }
        return 0;

    }

    public float getFloat()
    {
        switch (type)
        {
            case 0:
                return (bl?1:0);
            case 1:
                return b;
            case 2:
                return s;
            case 3:
                return i;
            case 4:
                return f;
        }
        return 0;
    }


    public Object get()
    {
        switch (type)
        {
            case 0:
                return bl?1:0;
            case 1:
                return b;
            case 2:
                return s;
            case 3:
                return i;
            case 4:
                return f;
        }
        return 0;
    }


}
